﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DosTexPrj.Core.RepositoriesInterface;
using DosTexPrj.Implement.NHibernate;
using DosTexPrj.UI.WinForm.Commons;
using DosTexPrj.Core.Models;

namespace DosTexPrj.UI.WinForm
{
    public partial class DeliveryMethodForm : Form
    {
        #region --- Private members ---

        private IRepository _repository;

        #endregion --- Private members ---

        #region --- Constructors ---

        public DeliveryMethodForm()
        {
            InitializeComponent();
            _repository = MyServiceLocator.DoGetInstance<IRepository>();
        }

        #endregion --- Constructors ---

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValidate())
                return;
            DeliveryMethod deliveryMethod = new DeliveryMethod
            {
                DeliveryMethodName = txtMethodName.Text.Trim()
            };

            try
            {
                this._repository.Save<DeliveryMethod>(deliveryMethod);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        #region Function

        private void ClearForm()
        {
            // Clear textboxes
            txtMethodName.Text = string.Empty;
        }

        private bool CheckValidate()
        {
            error.Clear();
            bool flag = true;
            if (!Common.IsBlankTextBox(txtMethodName, "Required field!", error))
                flag = false;
            return flag;
        }

        #endregion Functions
    }
}
